﻿/*
** GLOBALY USED JAVASCRIPT FOR BASKET ***    
HOW TO:
Just call using JavaScript:
...
if (typeof(Basket) == "object") Basket.Refresh();
...
This code above will send a Request to the server, 
to get the current User Basket Article List,
and should receive JSON results that will be displayed on the HTML page.
*/
var Basket = {

    LocalizedNumberDecimalSeparator: JsConst.LocalizedNumberDecimalSeparator,
    JsNumberDecimalSeparator: JsConst.JsNumberDecimalSeparator,

    GetUserBasketJsonContentUrl: JsConst.GetUserBasketJsonContentUrl,
    GetRemoveAllFromBasketUrl: JsConst.RemoveAllFromBasketUrl,

    Refresh: function () {
        //var json = $.toJSON(article);

        var self = this;

        $.ajax({
            url: self.GetUserBasketJsonContentUrl,
            cache: false,
            type: 'POST',
            dataType: 'json',
            //data: json,
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                Basket.GetUserBasket_callback(data);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                Basket.GetUserBasket_callback(xhr.status + ":" + thrownError, self);
            }
        });
    },

    GetUserBasket_callback: function (callBackData) {
        if (typeof (callBackData) == "object") {
            var basket = $("#" + JsConst.USER_BASKET_INFO_CONTROL_ID);
            //basket.effect("highlight", { color: "#701F78" }, 1000);
            // basket.toogle()
            var basketDropDown = $("#" + JsConst.USER_BASKET_DROP_DOWN_ID);

            basketDropDown.css("display", "none");

            var basketList = $("#" + JsConst.USER_BASKET_LIST);
            var basketTotalPrice = $("#" + JsConst.USER_BASKET_PRICE);
            var basketTotalArticles = $("#" + JsConst.USER_BASKET_ARTICLE);
            var basketHTML = "";
            var totalPrice = 0;
            var totalArticles = 0;
            var self = this;
            basketHTML += "<ul>";
            $.each(callBackData, function (index, data) {

                //For Properties Info see Shop.Models.ArticleOutputModel

                totalPrice += data.Price;
                totalArticles += 1;
                basketHTML += "<li title='" + data.Name + "'>" + self.FormatDecimal(data.Quantity) + "-" + data.NameShort + "-" + self.FormatDecimal(data.Price) + "PLN</li>\n";
            });
            basketHTML += "</ul>";
            basketList.html(basketHTML);
            basketTotalPrice.html(this.FormatDecimal(totalPrice) + " PLN");
            basketTotalArticles.html(totalArticles);

            if (totalPrice > 0) {
                basketDropDown.css("display", "block");
            }
        }
        else {
            alert("Error!");
        }
    },

    RefreshBig: function () {
        //var json = $.toJSON(article);

        var self = this;

        $.ajax({
            url: self.GetUserBasketJsonContentUrl,
            cache: false,
            type: 'POST',
            dataType: 'json',
            //data: json,
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                Basket.GetUserBasketBig_callback(data);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                Basket.GetUserBasket_callback(xhr.status + ":" + thrownError, self);
            }
        });
    },

    GetUserBasketBig_callback: function (callBackData) {
        if (typeof (callBackData) == "object") {
            var basket = $("#" + JsConst.USER_BASKET_INFO_CONTROL_ID);
            //basket.effect("highlight", { color: "#701F78" }, 1000);
            // basket.toogle()


            var basketList = $("#" + JsConst.USER_BASKET_LIST_BIG);
            var basketTotalPrice = $("#" + JsConst.USER_BASKET_PRICE_BIG);
            var basketTotalArticles = $("#" + JsConst.USER_BASKET_ARTICLE_BIG);
            var basketHTML = "";
            var totalPrice = 0;
            var totalArticles = 0;
            var self = this;
            basketHTML += "<div>";
            $.each(callBackData, function (index, data) {

                //For Properties Info see Shop.Models.ArticleOutputModel

                totalPrice += data.Price;
                totalArticles += 1;
                basketHTML += "<div>" + data.Name + " " + self.FormatDecimal(data.Quantity) + "-" + data.NameShort + "-" + self.FormatDecimal(data.Price) + "PLN</div>\n";
            });
            basketHTML += "</div>";
            basketList.html(basketHTML);
            basketTotalPrice.html(this.FormatDecimal(totalPrice) + " PLN");
            basketTotalArticles.html(totalArticles);

        }
        else {
            alert("Error!");
        }
    },

    FormatDecimal: function (price) {
        return price.toFixed(2).replace(".", this.LocalizedNumberDecimalSeparator).replace(",", this.LocalizedNumberDecimalSeparator);
    },
    RemoveAllFromBasket: function () {
        $.ajax({
            url: Basket.GetRemoveAllFromBasketUrl,
            cache: false,
            type: 'POST',
            dataType: 'json',
            //data: json,
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                location.reload(true);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                Basket.GetUserBasket_callback(xhr.status + ":" + thrownError, self);
            }
        });

    },
    /*
    order
    */
    PreOrderLoadBasket: function () {
        //var json = $.toJSON(article);

        var self = this;

        $.ajax({
            url: self.GetUserBasketJsonContentUrl,
            cache: false,
            type: 'POST',
            dataType: 'json',
            //data: json,
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                Basket.PreOrder_callback(data);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                Basket.PreOrder_callback(xhr.status + ":" + thrownError, self);
            }
        });
    },

    PreOrder_callback: function (callBackData) {
        if (typeof (callBackData) == "object") {
            var basket = $("#" + JsConst.USER_BASKET_ORDER_ID);
            //basket.effect("highlight", { color: "#701F78" }, 1000);
            // basket.toogle()

            var basketList = $("#" + JsConst.USER_BASKET_LIST);
            var basketTotalPrice = $("#" + JsConst.USER_BASKET_PRICE);
            var basketTotalArticles = $("#" + JsConst.USER_BASKET_ARTICLE);
            var basketHTML = "";
            var totalPrice = 0;
            var totalArticles = 0;
            var self = this;
            basketHTML += "<ul>";
            $.each(callBackData, function (index, data) {

                //For Properties Info see Shop.Models.ArticleOutputModel

                totalPrice += data.Price;
                totalArticles += 1;
                basketHTML += "<li title='" + data.Name + "'>" + data.NameShort + "<span class='price'>" + self.FormatDecimal(data.Quantity) + " " + self.FormatDecimal(data.Price) + "&euro;</span></li>\n";
            });
            basketHTML += "</ul>";
            basketList.html(basketHTML);
            basketTotalPrice.html(this.FormatDecimal(totalPrice) + "PLN;");
            basketTotalArticles.html(totalArticles);
        }
        else {
            alert("Error!");
        }
    }
}